/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.demis.elf;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author stephane
 */
public class ColumnReferencesHolder {

    private List<ColumnReference> referencedColumns = new ArrayList<ColumnReference>();

    public void addColumnReference(ColumnReference reference) {
        referencedColumns.add(reference);
    }

    public void addColumn(Column column, int order) {
        ColumnReference reference = new ColumnReference();
        reference.setColumn(column);
        reference.setOrder(order);
        reference.setColumnName(column.getName());
        reference.setTableName(column.getTable().getName());
        referencedColumns.add(reference);
    }

    public List<ColumnReference> getColumnReferences() {
        return referencedColumns;
    }

    public List<Column> getColumns() {
        List<Column> result = new ArrayList<Column>();

        for (ColumnReference reference: referencedColumns) {
            if (reference.getColumn() != null) {
                result.add(reference.getColumn());
            }
        }

        return result;
    }

    public int nbColumn() {
        return referencedColumns.size();
    }

    public Column getColumnAs(int pos) {
        for (ColumnReference reference: referencedColumns) {
            if (reference.getOrder() == pos) {
                return reference.getColumn();
            }
        }
        return null;
    }

    public Column getColumn(String columnName) {
        for (ColumnReference reference: referencedColumns) {
            if (reference.getColumn().getName().equals(columnName)) {
                return reference.getColumn();
            }
        }
        return null;
    }
}
